<template>
  <div>
    <el-card>
      <CategorySelector @changeCategory="changeCategory" :isShowCard="isShowCard"></CategorySelector>
    </el-card>
    <el-card style="margin-top: 20px">
      <div v-show="isShowCard">
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="addAttrInfo"
          :disabled="!category3Id"
          >添加属性</el-button
        >
        <el-table border :data="attrInfoList" style="width: 100%">
          <el-table-column type="index" width="80" align="center" label="序号">
          </el-table-column>

          <el-table-column prop="attrName" label="属性名称" width="150">
          </el-table-column>

          <el-table-column label="属性值列表" width="width">
            <template slot-scope="{ row, $index }">
              <el-tag
                type="success"
                v-for="(attrValue, index) in row.attrValueList"
                :key="attrValue.id"
              >
                {{ attrValue.valueName }}
              </el-tag>
            </template>
          </el-table-column>

          <el-table-column label="操作" width="150">
            <template slot-scope="{ row, $index }">
              <el-tooltip
                class="item"
                effect="dark"
                content="修改属性"
                placement="bottom"
              >
                <el-button
                  size="mini"
                  type="primary"
                  icon="el-icon-edit"
                  @click="updataAttr(row)"
                ></el-button>
              </el-tooltip>

              <el-popconfirm
                confirm-button-text="确定"
                cancel-button-text="取消"
                icon="el-icon-info"
                icon-color="red"
                :title="`确定删除:${row.attrName}?`"
                @onConfirm="delAttr(row.id)"
              >
                <el-button
                  style="margin-left:20px;"
                  slot="reference"
                  size="mini"
                  type="danger"
                  icon="el-icon-delete"
                ></el-button>
              </el-popconfirm>

            </template>
          </el-table-column>
        </el-table>
      </div>
      <div v-show="!isShowCard">
        <el-form :model="attrForm" :inline="true">
          <el-form-item label="属性名">
            <el-input v-model="attrForm.attrName"></el-input>
          </el-form-item>
        </el-form>

        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="addAttrValue"
          :disabled="!attrForm.attrName"
          >添加属性值</el-button
        >
        <el-button @click="cancelAddAttr">取消</el-button>

        <el-table
          border
          style="width: 100%; margin: 20px 0"
          :data="attrForm.attrValueList"
        >
          <el-table-column
            type="index"
            width="80"
            label="序号"
            align="center"
          ></el-table-column>

          <el-table-column property="date" label="属性值名称" width="width">
            <template slot-scope="{ row, $index }">
              <el-input
                v-model="row.valueName"
                placeholder="请输入属性值"
                v-if="row.isInput"
                @keyup.enter.native="toobserve(row)"
                @blur="toobserve(row)"
                :ref="$index"
              ></el-input>
              <span
                v-else
                style="
                  display: block;
                  width: 100%;
                  height: 100%;
                  cursor: pointer;
                "
                @click="toEdit(row, $index)"
                >{{ row.valueName }}</span
              >
            </template>
          </el-table-column>
          <el-table-column property="name" label="操作" width="width">
            <template slot-scope="{ row, $index }">
              <el-popconfirm
                confirm-button-text="确定"
                cancel-button-text="取消"
                icon="el-icon-info"
                icon-color="red"
                :title="`确定删除:${row.valueName}吗？`"
                @onConfirm="deleteAttr($index)"
              >
                <el-button
                  slot="reference"
                  type="danger"
                  icon="el-icon-delete"
                  size="mini"
                ></el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>

        <el-button type="primary" @click="save">保存</el-button>
        <el-button @click="cancelAddAttr">取消</el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
import cloneDeep from "lodash/cloneDeep";
export default {
  name: "Attr",
  data() {
    return {
      category1Id: "",
      category2Id: "",
      category3Id: "",
      attrInfoList: [],
      isShowCard: true,
      attrForm: {
        attrName: "",
        attrValueList: [
          // {
          //   "attrId": 0,
          //   "id": 0,
          //   "valueName": "string"
          // }
        ],
        categoryId: 0,
        categoryLevel: 0,
      },
    };
  },
  methods: {
    changeCategory({ categoryId, lveal }) {
      if (lveal === 1) {
        this.category1Id = categoryId;
        this.category2Id = "";
        this.category3Id = "";
        this.attrInfoList = [];
      } else if (lveal === 2) {
        this.category2Id = categoryId;
        this.category3Id = "";
        this.attrInfoList = [];
      } else {
        this.category3Id = categoryId;
        this.getAttrList();
      }
    },
    async getAttrList() {
      let { category1Id, category2Id, category3Id } = this;
      try {
        const result = await this.$API.attr.getAttrInfoList(
          category1Id,
          category2Id,
          category3Id
        );
        if (result.code === 200) {
          this.attrInfoList = result.data;
        } else {
          this.$message.error("网络连接超时");
        }
      } catch (error) {
        this.$message.error(error.message);
      }
    },
    //点击添加属性的回调
    addAttrInfo() {
      this.attrForm = {
        attrName: "",
        attrValueList: [],
        categoryId: this.category3Id,
        categoryLevel: 3,
      };
      this.isShowCard = false;
    },
    //取消添加的按钮
    cancelAddAttr() {
      this.isShowCard = true;
    },
    //点击添加属性值
    addAttrValue() {
      this.attrForm.attrValueList.push({
        attrId: this.attrForm.id,
        valueName: "",
        isInput: true,
      });
      this.$nextTick(() => {
        this.$refs[this.attrForm.attrValueList.length - 1].focus();
      });
    },
    //点击修改的
    updataAttr(row) {
      this.isShowCard = false;
      this.attrForm = cloneDeep(row);
      this.attrForm.attrValueList.forEach((item) => {
        // item.isInput = false;
        this.$set(item, "isInput", false);
      });
    },
    //输入属性值失去焦点或者回车事件
    toobserve(row) {
      if (!row.valueName.trim()) {
        this.$message.error("请输入内容");
        row.valueName = "";
        return;
      }
      let istext = this.attrForm.attrValueList.some((item) => {
        if (item !== row) {
          return item.valueName === row.valueName;
        }
      });
      if (istext) {
        this.$message.error("内容重复，请重新输入");
        row.valueName = "";
        return;
      }
      row.isInput = false;
    },
    //将属性值变为编辑模式
    toEdit(row, index) {
      row.isInput = true;
      this.$nextTick(() => {
        this.$refs[index].focus();
      });
    },
    //属性值点击删除的按钮
    deleteAttr(index) {
      this.attrForm.attrValueList.splice(index, 1);
    },
    //点击保存的回调
    async save() {
      //.获取要提交的数据
      let data = this.attrForm;
      //。对数据进行校验
      data.attrValueList = data.attrValueList.filter((item) => {
        if (item.valueName !== "") {
          return true;
        }
      });
      //提交
      try {
        await this.$API.attr.updateAttrInfo(data);
        this.getAttrList();
        this.$message.success("属性更新成功");
        this.isShowCard = true;
      } catch (error) {
        this.$message.success("网络连接超时，属性更新失败");
      }
      //删除按钮
    },
    async delAttr(id) {
      console.log(id);
      try {
        await this.$API.attr.delete(id);
          this.$message.success("已删除该属性");
          this.getAttrList();
      } catch (error) {
          this.$message.error("网络连接超时！删除失败！");
      }
    }
  },
};
</script>

<style lang="scss" scoped>
</style>